package com.fanrenqiji.modules.sys.service.impl;

import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.service.BaseCommonService;
import com.fanrenqiji.modules.sys.dao.RoleDao;
import com.fanrenqiji.modules.sys.entity.Role;
import com.fanrenqiji.modules.sys.entity.User;
import com.fanrenqiji.modules.sys.service.RoleService;
import com.fanrenqiji.modules.sys.utils.UserUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lijian E-mail:ljz0721cx@163.com
 * @version 创建时间：2015年1月21日 下午12:43:40
 * @description 系统管理，安全相关实体的管理类,包括用户、角色、菜单.
 */

@Service(value = "roleService")
@Transactional(readOnly = true)
public class RoleCommonServiceImpl extends BaseCommonService implements RoleService {
	@Autowired
	private RoleDao roleDao;

	/**
	 * 分页查询未做完
	 */
	@Override
	public Page<Role> find(Page<Role> page, Role role) {
		DetachedCriteria dc = roleDao.createDetachedCriteria();
		User crrentUser = UserUtils.getUser();
		if (crrentUser.isAdmin()) {
			return roleDao.find(page, dc);
		}
		return roleDao.find(page, dc);
	}
	
	@Override
	public List<Role> getRoleListByRoleType(String roleType) {
		DetachedCriteria dc = roleDao.createDetachedCriteria();
		dc.add(Restrictions.eq("roleType", roleType));
		dc.add(Restrictions.eq("delFlag", Role.DEL_FLAG_NORMAL));
		return roleDao.find(dc);
	}

	@Override
	public Role findByEnName(String enName) {
		DetachedCriteria dc = roleDao.createDetachedCriteria();
		dc.add(Restrictions.eq("delFlag", Role.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("enName", enName));
		return roleDao.find(dc).get(0);
	}

	@Override
	@Transactional
	public void delete(Long id) {
		Role role = roleDao.findOne(id);
		role.setDelFlag("1");
		roleDao.save(role);
	}

	@Override
	public Long findByOfficeBelowRole(String enName ,Long officeId){
		return roleDao.countByOfficeBelowRole(enName, officeId);
	}

}
